package com.zhang.network.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.dto.BaseResponse;
import com.zhang.network.constant.BizResponse;
import com.zhang.network.model.NetworkUser;
import com.zhang.network.model.User;
import com.zhang.network.model.network.NetworkUserAddDTO;
import com.zhang.network.model.network.NetworkUserVo;
import com.zhang.network.model.network.SecondNetworkQueryDTO;
import com.zhang.network.service.NetworkUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 人脉圈成员管理接口
 *
 * @author zhangshuaiyin
 * @since 2021-04-03
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/network/users")
public class NetworkUserController {

    @Resource
    NetworkUserService networkUserService;

    @GetMapping("/friends/{id}")
    public BaseResponse<List<NetworkUser>> getFriendsById(@PathVariable("id") Integer networkId) {
        return BizResponse.success(networkUserService.list(new QueryWrapper<NetworkUser>().eq("network_id", networkId)));
    }

    /**
     * 单个人脉圈内成员列表
     *
     * @param networkId
     * @return
     */
    @GetMapping("/{id}")
    public BaseResponse<List<NetworkUserVo>> getUsersById(@PathVariable("id") Integer networkId) {
        return BizResponse.success(networkUserService.getUsersByNetworkId(networkId));
    }

    @GetMapping("/second")
    public BaseResponse<Page<User>> getUsersOfNetworkAdmin(SecondNetworkQueryDTO dto) {
        return BizResponse.success(networkUserService.getSecondNetwork(dto));
    }

    /**
     * 分页查询人脉圈内成员列表
     *
     * @param dto
     * @return
     */
    /*
    @GetMapping()
    public BaseResponse<Page<NetworkUserVo>> pageUsersByNetwork(NetworkUserQueryDTO dto) {
        log.info("参数信息：{}", dto);
        return BizResponse.success(networkUserService.pageUsersByNetwork(dto));
    }*/

    /**
     * 人脉圈内不存在的成员
     *
     * @param networkId
     * @return
     */
    @GetMapping("/within/{id}")
    BaseResponse<List<User>> getUsersNotInNetworkById(@PathVariable("id") Integer networkId) {
        return BizResponse.success(networkUserService.getUsersNotInNetworkById(networkId));
    }

    /**
     * 给人脉圈添加好友
     *
     * @param dto
     * @return
     */
    @PostMapping()
    public BaseResponse<Object> saveUsersToNetwork(@RequestBody NetworkUserAddDTO dto) {
        networkUserService.insertNetworkUsers(dto);
        return BizResponse.success();
    }

    /**
     * 删除人脉圈中某个成员
     *
     * @return
     */
    @DeleteMapping("")
    public BaseResponse<Object> deleteUserOfNetwork(@RequestBody NetworkUser networkUser) {
        log.info("请求参数为：{}", networkUser);
        networkUserService.deleteUserOfNetwork(networkUser);
        return BizResponse.success();
    }
}

